<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="今日日期" prop="planDate">
        <el-date-picker
            v-model="queryParams.planDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生产日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="产地" prop="factoryId">
        <el-select v-model="queryParams.factoryId" placeholder="请选择" filterable @change="factoryChange">
          <el-option v-for="team in factoryList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择" filterable>
          <el-option v-for="team in teamList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="exportDailyReport">按条件导出日总结表</el-button>
      </el-form-item>
    </el-form>

    <el-descriptions class="margin-top" title="带边框列表" :column="3" :size="size" border>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          班组名称
        </template>
        {{ form.teamName }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-mobile-phone"></i>
          日期
        </template>
        {{ form.planDate }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          生产工时(H)
        </template>
        {{ form.field1 }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          出勤总人数(人)
        </template>
        {{ form.field2 }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          新员工出勤人数(人)
        </template>
        {{ form.field3 }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          新员工产能(套):
        </template>
        {{ form.field4 }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          日计划产能(套)=日产总数/6
        </template>
        {{ form.totalTodayCapacity }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          产能达成率(%)=入库数量/日产总数
        </template>
        {{ form.yieldRate }} %
      </el-descriptions-item>
    </el-descriptions>
    <el-divider></el-divider>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-form ref="form" :model="form" label-width="120px">-->
<!--        <el-row :gutter="5">-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="日期：" prop="planDate">-->
<!--               {{ form.planDate }}-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="组别：" prop="teamName">-->
<!--               {{ form.teamName }}-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="生产工时(H)：" prop="field1">-->
<!--               {{ form.field1 }}-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="出勤总人数(人)：" prop="field2">-->
<!--               {{ form.field2 }}-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="新员工出勤人数(人)：" prop="field3">-->
<!--               {{ form.field3 }}-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="新员工产能(套)：" prop="field4">-->
<!--              {{ form.field4 }}-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="日计划产能(套)：" prop="totalTodayCapacity">-->
<!--              {{ form.totalTodayCapacity }}-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="日实际产能(套)：" prop="totalTodayInStock">-->
<!--              {{ form.totalTodayInStock }}-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="产能达成率(%)：" prop="yieldRate">-->
<!--              {{ form.yieldRate }} %-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--      </el-form>-->
<!--    </el-row>-->

    <el-table v-loading="loading"
              border style="width:100%;"
              :data="daysubmitList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column label="订单合同号" align="center" prop="contractCode" width="120" />
      <el-table-column label="生产工单号" align="center" prop="workerorderCode" width="130"  />
      <el-table-column label="产品编码" align="center" prop="bomCode" />
      <el-table-column label="产品名称" align="center" prop="bomName" />
      <el-table-column label="产品规格" align="center" prop="bomStandard" />
      <el-table-column label="订单数量" align="center" prop="productNum" />
<!--      <el-table-column label="当日生产数量" align="center" prop="dailyCapacity" />-->
      <el-table-column label="当日良品数量" align="center" prop="goodNum" />
      <el-table-column label="当日不合格数量" align="center" prop="ungoodNum" />
      <el-table-column label="剩余未完成数量" align="center" prop="remainingCompletionNum" />
      <el-table-column label="当日入库数量" align="center" prop="todayInStock" />
      <!--<el-table-column label="预计完工时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listDaysubmit, getDaysubmit, delDaysubmit, addDaysubmit, updateDaysubmit,selectDailyProductionSummary,exportDailyProductionSummary } from "@/api/sm/produce/daysubmit/daysubmit";
import { listTeam,listFactory } from "@/api/system/dept";
export default {
  name: "Daysubmit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班组日生产数据上报表格数据
      daysubmitList: [],
      factoryList: [],
      teamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryName: null,
        factoryId: null,
        teamName: null,
        teamId: null,
        submitDate: null,
        productCode: null,
        productName: null,
        planDate: null,
      },
       // 查询参数
      queryParams1: {
        level: 3,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryId: [
          { required: true, message: "制造公司ID不能为空", trigger: "blur" }
        ],
        teamId: [
          { required: true, message: "制造班组ID不能为空", trigger: "blur" }
        ],
        submitDate: [
          { required: true, message: "上报日期不能为空", trigger: "blur" }
        ],
        productCode: [
          { required: true, message: "产品编码不能为空", trigger: "blur" }
        ],
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.initCurDate();
    // this.getList();
    this.getFactoryList();
  },
  methods: {
    initCurDate() {
      const currentDate = new Date();
      const year = currentDate.getFullYear();
      const month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); // 月份需要加1，然后确保是两位数
      const day = currentDate.getDate().toString().padStart(2, '0'); // 获取日并确保是两位数
      this.queryParams.planDate = `${year}-${month}-${day}`;
    },
     /** 查询订单信息列表 */
     getFactoryList() {
      this.loading = true;
      listFactory({"level":2}).then(response => {
        this.factoryList = response.data;
        this.queryParams.factoryId = this.factoryList[0].deptId;
        this.getTeamList(this.queryParams.factoryId);
        this.loading = false;
      });
    },
    factoryChange(){
      this.getTeamList(this.queryParams.factoryId);
    },
    /** 查询订单信息列表 */
    getTeamList(parentId) {
      this.loading = true;
      listTeam({"parentId":parentId}).then(response => {
        this.teamList = response.data;
        if (this.teamList) {
          this.queryParams.teamId = this.teamList[0].deptId;
        }
        this.getList();
        this.loading = false;
      });
    },
    /** 查询班组日生产数据上报列表 */
    getList() {
      this.loading = true;
      selectDailyProductionSummary(this.queryParams).then(response => {
        this.daysubmitList = response.rows;
        this.total = response.total;
        this.form = response.footer && response.footer.length>0 ? response.footer[0] : {};
        this.loading = false;
      });
    },
    exportDailyReport2() {
      exportDailyProductionSummary(this.queryParams).then(response => {
        let blob = new Blob([response], { type: "application/vnd.ms-excel" });
        let downloadElement = document.createElement("a");
        let href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = `车间组别（每日）生产情况总结表_${this.queryParams.planDate}`; //下载后文件名
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryName: null,
        factoryId: null,
        teamName: null,
        teamId: null,
        submitDate: null,
        productCode: null,
        productName: null,
        planNum: null,
        trueNum: 0,
        newwokerNum: 0,
        newwokerCapacity: 0,
        createDate: null,
        creator: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班组日生产数据上报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDaysubmit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班组日生产数据上报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDaysubmit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDaysubmit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除班组日生产数据上报编号为"' + ids + '"的数据项？').then(function() {
        return delDaysubmit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    exportDailyReport() {
      this.download(`/order/statistics/exportDailyProductionSummary/${this.queryParams.planDate}/${this.queryParams.factoryId}/${this.queryParams.teamId}`, {
        ...this.queryParams
      }, `车间组别（每日）生产情况总结表_${this.queryParams.planDate}.xlsx`)
    }
  }
};
</script>
